﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
//using System.Threading.Tasks;

namespace Able.Common.DEncrypt
{
    /// <summary>
    /// AES加解密类
    /// </summary>
    public class AESHelper
    {
        #region 加密
        /// <summary>
        /// 加密
        /// </summary>
        /// <param name="txt">文本</param>
        /// <returns>加密后的文本</returns>
        public static string Encrypt(string txt)
        {
            return Encrypt(ConfigHelper.GetConfigString("AESKey"), ConfigHelper.GetConfigString("AESIV"), txt);
        }

        /// <summary>
        /// 加密
        /// </summary>
        /// <param name="key">密钥</param>
        /// <param name="iv">向量</param>
        /// <param name="txt">文本</param>
        /// <returns>加密后的文本</returns>
        private static string Encrypt(string key, string iv, string txt)
        {
            byte[] keyArray = UTF8Encoding.UTF8.GetBytes(key);
            byte[] ivArray = UTF8Encoding.UTF8.GetBytes(iv);
            byte[] txtArray = UTF8Encoding.UTF8.GetBytes(txt);

            RijndaelManaged rDel = new RijndaelManaged();
            rDel.BlockSize = 128;
            rDel.KeySize = 128;
            rDel.Key = keyArray;
            rDel.IV = ivArray;
            rDel.Mode = CipherMode.CBC;
            rDel.Padding = PaddingMode.PKCS7;

            ICryptoTransform cTransform = rDel.CreateEncryptor();
            byte[] resultArray = cTransform.TransformFinalBlock(txtArray, 0, txtArray.Length);

            return Convert.ToBase64String(resultArray, 0, resultArray.Length);
        }
        #endregion

        #region 解密
        /// <summary>
        /// 解密
        /// </summary>
        /// <param name="txt">文本</param>
        /// <returns>解密后的文本</returns>
        /// <returns></returns>
        public static string Decrypt(string txt)
        {
            return Decrypt(ConfigHelper.GetConfigString("AESKey"), ConfigHelper.GetConfigString("AESIV"), txt);
        }

        /// <summary>
        /// 解密
        /// </summary>
        /// <param name="key">密钥</param>
        /// <param name="iv">向量</param>
        /// <param name="txt">文本</param>
        /// <returns>解密后的文本</returns>
        /// <returns></returns>
        private static string Decrypt(string key, string iv, string txt)
        {
            byte[] keyArray = UTF8Encoding.UTF8.GetBytes(key);
            byte[] ivArray = UTF8Encoding.UTF8.GetBytes(iv);
            byte[] txtArray = Convert.FromBase64String(txt);

            RijndaelManaged rDel = new RijndaelManaged();
            rDel.BlockSize = 128;
            rDel.KeySize = 128;
            rDel.Key = keyArray;
            rDel.IV = ivArray;
            rDel.Mode = CipherMode.CBC;
            rDel.Padding = PaddingMode.PKCS7;

            ICryptoTransform cTransform = rDel.CreateDecryptor();
            byte[] resultArray = cTransform.TransformFinalBlock(txtArray, 0, txtArray.Length);

            return UTF8Encoding.UTF8.GetString(resultArray);
        }
        #endregion
    }
}
